package com.surpassun.emall.service.impl;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.surpassun.emall.model.City;
import com.surpassun.emall.model.Country;
import com.surpassun.emall.model.User;
import com.surpassun.emall.service.CacheService;
import com.surpassun.emall.service.CityService;
import com.surpassun.emall.service.CountryService;
import com.surpassun.emall.service.UserService;
import com.surpassun.emall.util.CacheUtil;
import com.surpassun.emall.util.Constants;
import com.surpassun.emall.util.Validator;

@Service
public class CacheServiceImpl implements CacheService {
	
	@Inject
	UserService userService;
	
	@Inject
	CountryService countryService;
	
	@Inject
	CityService cityService;

	@SuppressWarnings("unchecked")
	@Override
	public List<User> getUserList() {
		List<User> users = (List<User>)CacheUtil.getApplicationCache().get(Constants.ATTR_USERS);
		if (Validator.isNull(users)) {
			users = userService.getAll();
			CacheUtil.getApplicationCache().put(Constants.ATTR_USERS, users);
		}
		
		return users;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Country> getCountryList() {
		List<Country> countries = (List<Country>)CacheUtil.getApplicationCache().get(Constants.ATTR_COUNTRIES);
		if (Validator.isNull(countries)) {
			countries = countryService.getAll();
			CacheUtil.getApplicationCache().put(Constants.ATTR_COUNTRIES, countries);
		}
		
		return countries;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<City> getCityList() {
		List<City> cities = (List<City>)CacheUtil.getApplicationCache().get(Constants.ATTR_CITIES);
		if (Validator.isNull(cities)) {
			cities = cityService.getAll();
			CacheUtil.getApplicationCache().put(Constants.ATTR_CITIES, cities);
		}
		
		return cities;
	}

}
